//
//  CountButton.m
//  TestProject
//
//  Created by 李萍 on 2017/4/13.
//  Copyright © 2017年 李萍. All rights reserved.
//

#import "CountButton.h"
#import "UIColor+CustomColor.h"

#define viewHeight self.bounds.size.height
#define viewWidth  self.bounds.size.width
#define lineWidth 1

typedef void(^CountBtnChoosedBlock)(NSUInteger countNum);

@interface CountButton ()

@property (nonatomic, copy) CountBtnChoosedBlock countBtnChooseBlock;

@property (nonatomic, weak) UIButton *subButton;
@property (nonatomic, weak) UIButton *addButton;
@property (nonatomic, weak) UITextField *countTextField;
    
@property (nonatomic, weak) UIView *leftLine;
@property (nonatomic, weak) UIView *rightLine;

@end

@implementation CountButton

+ (instancetype)countBtnWithChooseBlock:(void(^)(NSUInteger countNum))countBtnChoosedBlock
{
    CountButton *countBtn= [CountButton new];
    countBtn.backgroundColor = [UIColor clearColor];
    countBtn.countBtnChooseBlock = ^(NSUInteger countNum) {
        if (countBtnChoosedBlock) {
            countBtnChoosedBlock(countNum);
        }
    };

    return countBtn;
}

- (void)drawRect:(CGRect)rect
{
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIButton *subBtn = [UIButton new];
        [subBtn setImage:[UIImage imageNamed:@"sub"] forState:UIControlStateNormal];
        self.subButton = subBtn;
        [self addSubview:subBtn];
        
        UIButton *addBtn = [UIButton new];
        [addBtn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        self.addButton = addBtn;
        [self addSubview:addBtn];
        
        UITextField *countTF = [UITextField new];
        countTF.backgroundColor = [UIColor yellowColor];
        self.countTextField = countTF;
        [self addSubview:countTF];
        
        UIView *lLine = [UIView new];
        self.leftLine = lLine;
        self.leftLine.backgroundColor = [UIColor themeColor];
        [self addSubview:lLine];
        
        UIView *rLine = [UIView new];
        self.rightLine = rLine;
        self.rightLine.backgroundColor = [UIColor themeColor];
        [self addSubview:rLine];
    }
    
    return self;
}
    
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.subButton.frame = CGRectMake(0, 0, viewHeight, viewHeight);
    self.leftLine.frame = CGRectMake(viewHeight, 0, lineWidth, viewHeight);
    self.countTextField.frame = CGRectMake(viewHeight+lineWidth, 0, viewWidth- 2*viewHeight-2*lineWidth, viewHeight);
    self.rightLine.frame = CGRectMake(viewWidth-viewHeight-lineWidth, 0, lineWidth, viewHeight);
    self.addButton.frame = CGRectMake(viewWidth-viewHeight, 0, viewHeight, viewHeight);
    
    if (_countNum && _countNum > 1) {
        self.countTextField.text = [NSString stringWithFormat:@"%d", _countNum];
    } else {
        self.countTextField.text = @"0";
    }
    self.countTextField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.countTextField.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
    self.subButton.tag = 1;
    self.addButton.tag = 2;
    [self.subButton addTarget:self action:@selector(countChange:) forControlEvents:UIControlEventTouchUpInside];
    [self.addButton addTarget:self action:@selector(countChange:) forControlEvents:UIControlEventTouchUpInside];
}
    
- (void)countChange:(UIButton *)button
{
    NSLog(@"click %ld", (long)button.tag);
    if (_delegate && [_delegate respondsToSelector:@selector(countButtonCountChange:)]) {
        [_delegate countButtonCountChange:button.tag];
    }
}

@end
